<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head th:include="head::htmlhead">
</head>
<body>
<div class="layuimini-container ">
    <div class="layuimini-main">
        <fieldset class="layui-elem-field layui-field-title">
            <legend>输入字典信息</legend>
        </fieldset>
        <form class="layui-form" action="">
            <input style="display: none" id="id" name="id">
            <div class="layui-form-item" style="">
                <div class="layui-inline">
                    <label class="layui-form-label">字典类别</label>
                    <div class="layui-input-block">
                        <select id="typeName" name="typeName" lay-filter="typeName">
                            <option value=""></option>
                        </select>
                    </div>
                </div>

                <div class="layui-inline">
                    <label class="layui-form-label">类别编号</label>
                    <div class="layui-input-block">
                        <input type="text" id="typeCode" name="typeCode" placeholder="请先选择字典类别" autocomplete="off"
                               class="layui-input" readonly="readonly" required lay-verify="required">
                    </div>
                </div>
            </div>
            <div class="layui-form-item ">
                <div class="layui-inline">
                    <label class="layui-form-label">字典类别选项</label>
                    <div class="layui-input-block">
                        <input type="text" id="itemName" name="itemName" placeholder="请输入新选项" autocomplete="off"
                               class="layui-input">
                    </div>
                </div>
            </div>
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">序号</label>
                    <div class="layui-input-block">
                        <input type="text" id="dictSort" name="dictSort" placeholder="显示顺序(默认顺序排序)"
                               autocomplete="off" class="layui-input">
                    </div>
                </div>
            </div>
            <div class="layui-form-item layui-form-text">
                <label class="layui-form-label">备注</label>
                <div class="layui-input-block">
                    <textarea id="remark" name="remark" class="layui-textarea" placeholder="请输入备注信息"></textarea>
                </div>
            </div>

            <div class="layui-form-item ">
                <div class="layui-input-block">
                    <button class="layui-btn" lay-submit lay-filter="DictForm">立即提交</button>
                    <button type="reset" class="layui-btn layui-btn-primary">重置</button>
                </div>
            </div>
        </form>
    </div>
</div>
<script>
    layui.use('form', function () {
        var form = layui.form, layer = layui.layer, $ = layui.jquery;
        //console.log(parent.layer.index)
        $.ajax({
            url: "getAllTypeName",
            type: "get",
            success: function (result) {
                //console.log(result);
                var typeName = document.getElementById("typeName");
                for (var i = 0; i < result.length; i++) {
                    var option = document.createElement("option");
                    option.setAttribute("value",result[i].typeCode);
                    option.innerText=result[i].typeName;
                    typeName.appendChild(option);
                }
                form.render("select");

                if(getQueryString('id')!=null){
                    var id=getQueryString('id');

                    $("#id").val(id);
                    $.ajax({
                       url:"getBaseDictById",
                       data:{"id":id},
                        type:"post",
                       success:function (result) {
                           $("#typeCode").val(result.typeCode);
                           $("#typeName").val(result.typeCode);
                           $("#itemName").val(result.itemName);
                           $("#dictSort").val(result.dictSort);
                           $("#remark").val(result.remark);
                           form.render();
                       }
                    });
                    //if(getQueryString('typeCode')!=null) $("#typeCode").val(getQueryString('typeCode'));
                   // if(getQueryString('typeName')!=null) $("#typeName").val(getQueryString('typeCode'));
                    //if(getQueryString('itemName')!=null) $("#itemName").val(getQueryString('itemName'));
                    //if(getQueryString('dictSort')!=null) $("#dictSort").val(getQueryString('dictSort'));
                    //if(getQueryString('remark')!=null) $("#remark").val(getQueryString('remark'));

                    //console.log(document.getElementById('id').value);
                }
            }
        });

        form.on('select(typeName)', function(data){

            $("#typeCode").val(data.value)
            //console.log($("#typeName").find("option:selected").text())

        });
        form.on('submit(DictForm)', function (data) {
            /*layer.msg(JSON.stringify(data.field));
            return false;*/
            layer.confirm("是否提交",function () {
                $.ajax({
                    url: "saveDict",
                    data: {
                        "id": data.field.id
                        , "typeCode": data.field.typeCode
                        , "typeName": $("#typeName").find("option:selected").text()
                        , "itemName": data.field.itemName
                        , "dictSort": data.field.dictSort
                        , "remark": data.field.remark
                    },
                    //dataType: 'JSON',
                    type: 'POST',
                    success: function (data) {
                        //console.log("添加成功");
                        layer.msg(data.msg,  {
                            icon: 1,
                            time: 2000 //2秒关闭（如果不配置，默认是3秒）
                        },  function () {
                                //layer.closeAll();
                                parent.layer.close(parent.layer.index);
                                parent.layui.table.reload('currentTableId',{})
                            })
                    },
                    error: function (XMLHttpRequest, textStatus, errorThrown) {
                        alert(XMLHttpRequest.responseText + '--------' + textStatus + '++++++' + errorThrown + '------' + "异常信息，稍后重试");
                    }
                });
            });
            return false;
        });

        function getQueryString(name) {
            var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
            var r = window.location.search.substr(1).match(reg);
            //if (r != null) return unescape(r[2]);
            if (r != null) return decodeURI(r[2]);
            return null;
        }
    });
</script>
</body>
</html>
